package cn.com.cfae.iras.doc.analyze.parse.extractor;

import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractBizModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractItem;
import cn.com.cfae.iras.doc.analyze.parse.model.PageModel;
import com.google.gson.JsonArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * M13抽取
 */
public class M13Extractor extends MTableExtractor {
    private static final Logger logger = LoggerFactory.getLogger(M13Extractor.class);

    @Override
    public void extract() {
        try {
            extractM13();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    private void extractM13() {
        long t1 = System.currentTimeMillis();
        logger.info("开始抽取：{}章业务模型数据......", getChapterModel().getChapterId());
        try {
            extractM13Zcxs();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        try {
            extractM13Fxr();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        long t2 = System.currentTimeMillis();
        logger.info("抽取：{}章业务模型数据完成，耗时：{}毫秒。", getChapterModel().getChapterId(), (t2 - t1));
    }


    private void extractM13Zcxs() {
        long t1 = System.currentTimeMillis();
        ExtractBizModel extractBizModel = getChapterModel().getExtractBizModelMap().get("ZCXS");
        logger.info("开始抽取：{}章主承销商业务模型......", getChapterModel().getChapterId());
        String zcxc = null;
        ExtractBizModel var1 = getDocumentModel().getChapterModel(getBaseTable().getTableType() + "-0").getExtractBizModelMap().get("ZCXS");
        if (var1 == null) {
            return;
        } else {
            ExtractItem extractItem = var1.getExtractItem("MC");
            if (extractItem == null) {
                return;
            }
            if (var1.getExtractItem("MC").getValue() == null) {
                return;
            }
            zcxc = var1.getExtractItem("MC").getValue().getText();
        }
        if (isEmpty(zcxc)) {
            return;
        }
        logger.info("抽取：{}章主承销商业务模型，M-0/BM-0，主承销商名称：{}。", getChapterModel().getChapterId(), zcxc.trim());
        List<PageModel> pageModelList = getChapterModel().getPageModelList();
        ExtractItem extractItem = null;
        PageModel pageModel = null;
        for (int i = 0; i < pageModelList.size(); i++) {
            pageModel = pageModelList.get(i);
            JsonArray dataArray = getPageDataArray(pageModel);
            for (int j = 0; j < dataArray.size(); j++) {
                JsonArray itemArray = dataArray.get(j).getAsJsonArray();
                String combineLine = combineTextJsonArray(itemArray);
                if (isEmpty(combineLine)) {
                    continue;
                }
                if (combineLine.contains(zcxc)) {
                    logger.info("抽取：{}章主承销商业务模型，分析到发行人名称，当前页码：{}，分析内容：{}。", getChapterModel().getChapterId(), pageModel.getPageNumber(), combineLine);
                    extractItem = extractBizModel.getExtractItem("MC");
                    KVText kText = new KVText();
                    kText.setText(extractItem.getSynonyms()[0]);
                    kText.add(createKVTextLayout(pageModel, itemArray));
                    KVText vText = new KVText();
                    vText.setText(combineLine);
                    vText.add(createKVTextLayout(pageModel, itemArray));
                    extractItem.setKey(kText);
                    extractItem.setValue(vText);
                    break;
                }
            }
            if (extractItem != null) {
                break;
            }
        }
        long t2 = System.currentTimeMillis();
        logger.info("抽取：{}章主承销商业务模型，耗时：{}毫秒。", getChapterModel().getChapterId(), (t2 - t1));
    }

    private void extractM13Fxr() {
        long t1 = System.currentTimeMillis();
        ExtractBizModel extractBizModel = getChapterModel().getExtractBizModelMap().get("FXR");
        logger.info("开始抽取：{}章发行人业务模型数据......", getChapterModel().getChapterId());
        String fxrmc = null;
        ExtractBizModel var1 = getDocumentModel().getChapterModel(getBaseTable().getTableType() + "-0").getExtractBizModelMap().get("FXR");
        if (var1 == null) {
            return;
        } else {
            ExtractItem extractItem = var1.getExtractItem("FXRMC");
            if (extractItem == null) {
                return;
            }
            if (var1.getExtractItem("FXRMC").getValue() == null) {
                return;
            }
            fxrmc = var1.getExtractItem("FXRMC").getValue().getText();
        }
        if (isEmpty(fxrmc)) {
            return;
        }
        logger.info("抽取：{}章发行人业务模型，M-0/BM-0发行人名称：{}。", getChapterModel().getChapterId(), fxrmc);
        List<PageModel> pageModelList = getChapterModel().getPageModelList();
        ExtractItem extractItem = null;
        PageModel pageModel = null;
        for (int i = 0; i < pageModelList.size(); i++) {
            pageModel = pageModelList.get(i);
            JsonArray dataArray = getPageDataArray(pageModel);
            for (int j = 0; j < dataArray.size(); j++) {
                JsonArray itemArray = dataArray.get(j).getAsJsonArray();
                String combineLine = combineTextJsonArray(itemArray);
                if (isEmpty(combineLine)) {
                    continue;
                }
                if (isProspectus(combineLine)) {
                    continue;
                }
                if (combineLine.contains(fxrmc)) {
                    logger.info("抽取：{}章发行人业务模型，分析到发行人名称，当前页码：{}，分析内容：{}。", getChapterModel().getChapterId(), pageModel.getPageNumber(), combineLine);
                    extractItem = extractBizModel.getExtractItem("FXRMC");
                    KVText kText = new KVText();
                    kText.setText(extractItem.getSynonyms()[0]);
                    kText.add(createKVTextLayout(pageModel, itemArray));
                    KVText vText = new KVText();
                    vText.setText(combineLine);
                    vText.add(createKVTextLayout(pageModel, itemArray));
                    extractItem.setKey(kText);
                    extractItem.setValue(vText);
                    break;
                }
            }
            if (extractItem != null) {
                break;
            }
        }
        long t2 = System.currentTimeMillis();
        logger.info("抽取：{}章发行人业务模型完成，耗时：{}毫秒。", getChapterModel().getChapterId(), (t2 - t1));
    }
}
